package com.qph.app.system.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qph.app.system.service.IUserManager;
import com.qph.base.exception.BusinessException;
import com.qph.base.model.Pagination;
import com.qph.base.security.entity.User;
import com.qph.utils.base.StringUtil;

/**
 * 用户管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value="/user")
public class UserController {

	@Resource(name="userService")
	private IUserManager userService;
	
	/**
	 * 打酱油之用户管理
	 * 权限：User:query
	 * @return
	 */
	@RequestMapping(value="/userList")
	@RequiresPermissions("User:query")
	public String userList(){
		
		return "/system/user/userList";
	}
	
	/**
	 * ajax分页查询用户列表
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getUserList")
	@RequiresPermissions("User:query")
	public Pagination getUserList(User user){
		
		return userService.getPagedList(user);
	}
	
	
	/**
	 * 按角色查询未授予的用户
	 * @param roleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getWfpUserList")
	public Pagination getWfpUserListForRole(String roleId){
		
		return userService.getWfpUserListForRole(roleId);
	}
	
	/**
	 * 按角色查询已授予的用户
	 * @param roleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getYfpUserList")
	public Pagination getYfpUserListForRole(String roleId){
		return userService.getYfpUserListForRole(roleId);
	}
	
	
	
	/**
	 * 跳转到新建用户界面~~打酱油
	 * @return
	 */
	@RequestMapping(value="/addUser")
	@RequiresPermissions("User:add")
	public String addUser(){
		
		return "/system/user/addUser";
	}
	
	
	/**
	 * 跳转到修改用户界面
	 * @return
	 */
	@RequestMapping(value="/editUser")
	@RequiresPermissions("User:edit")
	public String editUser(HttpServletRequest request, String username){
		
		User user = userService.getModel(username);
		request.setAttribute("user", user);
		return "/system/user/editUser";
	}
	
	
	/**
	 * 保存用户
	 * @param user
	 * @return
	 * @throws BusinessException 
	 */
	@ResponseBody
	@RequestMapping(value="/saveUser")
	public JSONObject saveUser(User user) throws BusinessException{
		boolean result = false;
		
		JSONObject json = new JSONObject();
		
		if (StringUtil.isEmpty(user.getUserid())){
			try {
				result = userService.insert(user);
			} catch (BusinessException e) {
				json.put("message", e.getMessage());
			}
		} else {
			result = userService.update(user);
		}
		
		json.put("result", result);
		return json;
	}
	
	
	
	
	/**
	 * 按用户名查找用户是否存在
	 * @param username
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getUserById")
	public User getUserById(String username){
		return userService.getModel(username);
	}
	
	
	/**
	 * 删除用户
	 * @param role
	 * @return
	 * @throws BusinessException 
	 */
	@ResponseBody
	@RequestMapping(value="/delUser")
	@RequiresPermissions("User:delete")
	public JSONObject delRole(String userid) throws BusinessException{
		int delCount = userService.delete(userid);
		JSONObject json = new JSONObject();
		json.put("result", delCount > 0);
		return json;
	}}
